package alipay.openplatform.edu.campus.jiuye.common.page;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
 * @author lis
 * @ClassName: BaseControl
 * @Description: TODO(Base类)
 * @date 2014-8-29 上午11:41:31
 */
public class PageUtil {
    private static int pageSize = 10;//每页条数
    private int pageNumber = 1;//

    /**
     * @param @return
     * @return HttpSession
     * @throws
     * @Title: getSession
     * @Description: TODO 取session
     * @author lis
     */
    public static HttpSession getSession() {
        HttpSession session = getRequest().getSession();
        return session;
    }

    /**
     * @param @return
     * @return HttpServletRequest
     * @throws
     * @Title: getRequest
     * @Description: TODO 取request
     * @author lis
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        return attrs.getRequest();
    }


    /**
     * @param @param  request
     * @param @return
     * @return Page<T>
     * @throws
     * @Title: buidPagebean
     * @Description: TODO
     * @author wangwl
     */
    public static Page buidPagebean(HttpServletRequest request, Map<String, Object> map) {
        if (map == null) {
            map = new HashMap<>();
        }
        int pageNumber = 1;
        int pageSize = 10;
        if (request.getParameter("pageNo") != null) {
            pageNumber = Integer.parseInt(request.getParameter("pageNo").trim());//页码
        }
        if (request.getParameter("pageSize") != null) {
            pageSize = Integer.parseInt(request.getParameter("pageSize").trim());//每页显示几条
        }
        Iterator it = request.getParameterMap().keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            String values[] = request.getParameterValues(key);
            if (values.length == 1) {
                map.put(key, values[0].trim());
            } else {
                map.put(key, values);
            }
        }
        Page pagebean = new Page();
        pagebean.setPageNo(pageNumber);
        pagebean.setPageSize(pageSize);
        pagebean.setParams(map);

        return pagebean;
    }
}
